import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from db.db_util import DBUtil
from entities.admin import Admin

class AdminDao:
    def login(self, admin_name, password):
        conn = None
        cursor = None
        try:
            conn = DBUtil.get_connection()
            cursor = conn.cursor()
            
            # 关键修改：使用数据库实际的列名adminName
            sql = "SELECT * FROM admin WHERE adminName = %s AND password = %s"
            cursor.execute(sql, (admin_name, password))
            
            result = cursor.fetchone()
            if result:
                return Admin(
                    admin_id=result['adminId'],      # 注意这里是adminId
                    admin_name=result['adminName'],  # 注意这里是adminName
                    password=result['password']
                )
            return None
            
        except Exception as e:
            print(f"登录错误: {e}")
            return None
        finally:
            DBUtil.close(conn, cursor)